package CTCOffice;

import java.util.ArrayList;
import java.util.ListIterator;

import TrackModel.StationBlock;

public class Schedule {
	private ArrayList<Destination> destinations;
	private int iTrainID;
	
	public Schedule(int tid, ArrayList<StationBlock> route, String aTime) {
		this.iTrainID = tid;
		destinations = new ArrayList<Destination>();
		
		for(StationBlock b : route)
			addDestination(((StationBlock)b).getBlockID(), aTime);
	}
	
	private void generate() {
		Destination prev = destinations.get(0);
		for(int i = 1; i < destinations.size(); i++) {
			String arrive = prev.getArrival();
			if(arrive != "")
				return;
		}
	}
	
	public void addDestination(int sid) {
		destinations.add(new Destination(sid));
	}
	
	public void addDestination(int sid, String t) { 
		destinations.add(new Destination(sid, t)); 
	}
	
	public ArrayList<Destination> getStations() {
		return this.destinations;
	}
	
	public void updateStartTime(String t) {
		destinations.get(0).setArrival(t);
		generate();
	}
	
	public int getTrainID() { return this.iTrainID;	}

	public void removeDestination(int sid) {
		ListIterator<Destination> itr = destinations.listIterator();
		while(itr.hasNext()) {
			Destination current = (Destination)itr.next();
			if(current.getID() == sid) {
				destinations.remove(current);
			}
		}
	}

	public String toString() {
		String out = "";
		ListIterator<Destination> itr = destinations.listIterator();

		while(itr.hasNext()) {
			Destination current = (Destination)itr.next();
			out += current.getID() + " -> ";
		}

		return out;
	}

	public class Destination {
		private int iStationID;
		private String lTime;

		public Destination(int sid) {
			this.iStationID = sid;
		}
		public Destination(int sid, String t) {
			this.iStationID = sid;
			this.lTime = t;
		}

		public int getID() {
			return this.iStationID; 
		}

		public String getArrival() {
			return this.lTime; 
		}

		public void setArrival(String t) {
			this.lTime = t; 
		}

	}
}